<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共 123 条记录</span>
        <template slot="after">
          <el-button size="small" type="warning">导入</el-button>
          <el-button size="small" type="danger">导出</el-button>
          <el-button size="small" type="primary">新增员工</el-button>
        </template>
      </page-tools>
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="手机号" sortable="" prop="mobile" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formatEmployment" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
            <template slot-scope="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="">
            <template slot-scope="{row}">
              <el-switch :value="row.enableState === 1" />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination layout="prev, pager, next" :total="page.total" @current-change="currentChange" />
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
// 引入 api
import { getEmployeeList } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
export default {
  data() {
    return {
      // 数据列表
      list: [],
      // 定义分页属性
      page: {
        page: 1,
        size: 10,
        total: 0
      }
    }
  },
  created() {
    // 初始化时调用函数获取数据, 默认是第一页, 十条数据
    this.getEmployeeList()
  },
  methods: {
    // 获取数据函数
    // 获取完页面数据后分别将数据放入 list 总条数放入 page.total
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList(this.page)
      console.log(rows, total)
      this.list = rows
      this.total = total
    },
    // 当用户点击翻页时会触发, 接收到用户选择的新页码
    // 设定翻页数据, 重新加载数据即可
    currentChange(newPage) {
      this.page.page = newPage
      this.getEmployeeList()
    },
    // 定义聘用形式的格式化函数
    formatEmployment(row, column, cellValue, index) {
      // 这个地方是对当前的列数据进行格式化,
      // 要变成什么内容, 直接 return 即可
      // 假如这里聘用形式是 1 === '正式工' 2 === '临时工'
      // return cellValue === 1 ? '正式工' : '临时工'
      // const dict = {
      //   '1': '新手',
      //   '2': '正式',
      //   '3': '尊贵',
      //   '4': '区域代理'
      // }

      // // 假设 cellValue === 1
      // return dict[cellValue]
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue)
      // console.log(obj)
      return obj.value
    }
  }
}
</script>

<style>

</style>
